<?php
require_once 'common.php';
require_once 'dataCenter.php';
require_once 'MJDMTask.php';
require_once 'MJDMUser.php';
log_action("In Page : ".__FILE__);

if(!isset($_SESSION)){session_start();}

$uid = GetSessionUID();

//the message return to html page
$response = "A new task was added!";
	
//get the parameter from post data
$method = $_POST["method"];
$id =isset($_POST["id"])?$_POST["id"]:"";
$taskname= isset($_POST["name"])?$_POST["name"]:"";
$points = isset($_POST["points"])?$_POST["points"]:"";
$type = isset($_POST["type"])?$_POST["type"]:"";
$dueDate = isset($_POST["dueDate"])?$_POST["dueDate"]:"";
$tag = isset($_POST["tag"])?$_POST["tag"]:"";
$url = isset($_POST["url"])?$_POST["url"]:"";

//log_action("task_operation...");
//log_action("method:".$method);

$task = new Task();

if($method == "add" or $method == "edit") 
{
	if(strlen($taskname) <= 0 )
	{
		echo "Empty task name!" ;
		return;
	}
	if(strlen($points) <= 0)
	{
		echo "No points !";
		return ;
	}
	
	if(strlen($type) <= 0 )
	{
		echo "No type !";
		return;
	}
	if(strlen($dueDate) <= 0)
	{
		echo "No due date !";
		return;
	}
	
	$task->id = $id;
	$task->name = $taskname;
	$task->points = $points;
	$task->type = $type;
	$task->dueDate = $dueDate;
	$task->tag =$tag;
	$task->uid = $uid;
	$task->url = $url;
}

//var_dump($task);


switch($method)
{
	case "add":
		AddTask($task);
		break;
	case "edit":
		UpdateTask($task);
		$response ="You've successfully changed the task settings!";
		break;
	case "delete":
		DeleteTask($id);
		$response = "The task was deleted!";
		break;
}


//return value
echo $response;

?>